package lib.tree;

/**
 * Binary tree node structure
 * 
 * @author <a href="http://www.sureinterview.com">sureinterview</a>
 * 
 */
public class BNode {
	public BNode left;
	public BNode next;
	public BNode parent;
	public BNode prev;
	public BNode right;
	public Integer size;
	public Integer value;

	public boolean isLeaf() {
		return left == null && right == null;
	}

	public String toListStr() {
		String result = "";
		BNode nd = this;
		do {
			result += (nd.value == null ? "null" : nd.value.toString()) + ", ";
			nd = nd.next;
		} while (nd != null);
		return result;
	}

	@Override
	public String toString() {
		return "Tree:" + toTreeStr() + "\nList: " + toListStr();
	}

	public String toTreeStr() {
		if (left == null && right == null)
			return (value == null ? "" : value.toString());

		String result = (value == null ? "" : value.toString()) + "(";
		if (left != null) {
			result += left.toTreeStr();
		}
		result += ",";
		if (right != null) {
			result += right.toTreeStr();
		}
		result += ")";
		return result;
	}
}
